import React from 'react';
import { connect } from 'dva';
import { Layout } from 'antd';
import BaseComponent from 'components/BaseComponent';
import BannerMng from 'components/BannerMng';
const { Content } = Layout;

@connect(({banners, loading})=> ({
  banners,
  loading: loading.models.banners,
}))
export default class extends BaseComponent {
  state = {};

  onChange = (type, item, i) => {
    switch (type) {
      case 'add':
        this.props.dispatch({
          type: 'banners/save',
          payload:{
            item
          }
        });
        break;
      case 'edit':
        this.props.dispatch({
          type: 'banners/update',
          payload:{
            item
          }
        });
        break;
      case 'delete':
        this.props.dispatch({
          type: 'banners/remove',
          payload:{
            item
          }
        });
        break;
      default:
        break;
    }
  };

  render() {
    const {banners, loading, dispatch} =this.props;
    const { bannerList } = banners;

    const formCols = [
      {
        title: 'ID',
        name: 'id',
        formItem: {type: 'hidden'}
      },
      {title: '标题',
        name: 'title',
        formItem: {rules: []}
      },{
        title: "链接",
        name: 'url',
        formItem: {rules: []}
      },{
        title: "链接类型",
        name: 'linkType',
        formItem: {
          type: 'select',
        },
        dict: [{code: 1, codeName: '无链接' }, {code: 2, codeName: '外部链接'}, { code: 3, codeName: '内部文章'}, {code: 4, codeName: '内部课程'}]
      },{
        title: '排序',
        name: 'sort',
        formItem: {
          type: 'number',
          rules: []
        }
      },{
        title: '图片',
        name: 'image',
        formItem: {
          type: 'upload',
          fileTypes: ['.png', '.jpg', '.gif'],
          listType: 'picture',
          action:"/api/banner/upload/bannerImage",
          fileName: 'file',
          normalize: value => {
            return [
              {
                uid: -1,
                thumbUrl: value
              }
            ];
          },
          max:1,
          multiple: false,
          rules: []
        },
      }
    ];

    return (
      <Layout className="full-layout page">
        <Content>
          <BannerMng dataSource={bannerList} onChange={this.onChange} formCols={formCols} />
          {/*<br />*/}
          {/*<BannerMng onChange={this.onChange} />*/}
        </Content>
      </Layout>
    );
  }
}
